<?php
namespace app\controller;

use app\BaseController;
use think\Log;
use think\response\Json;

class Upload extends BaseController
{
    public function index(): string
    {
        return 'Hello';
    }
    /**
     * 前端上传接口
     *
     */
    public function Upload(): Json
    {
        $file = $_FILES['file'];
        $uid = $this->getUid();
        if($file['error']>0){
            return pr(300,"文件上传出错");
        }
        if(empty($uid)) return pr(304,'请先登陆');
        $arr = explode('.',$file['name']);
        if(count($arr)==0) return pr(301,"上传失败");
        else{
            $ext = $arr[count($arr)-1];
            if($ext=="php"||$ext=="sql"||$ext=="exe"||$ext=="sh"||$ext=="bash"||$ext=="bat") return pr(302,'不支持的文件格式');
            $content = file_get_contents($file['tmp_name']);
            $p = saveToFile($file['name'],$content,$uid);
            $fileSize = filesize($file['tmp_name']);
            unlink($file['tmp_name']);
            $u = $this->M("files")->field('id')->where(['uid'=>$uid,'path'=>$p])->find();
            if(empty($u)){
                $fid = $this->M("files")->insert(['size'=>$fileSize,'uid'=>$uid,'type'=>0,'path'=>$p,'name'=>$file['name'],'create_time'=>time(),'last_modify_time'=>time(),'down_times'=>0],true);
            }else{
                $fid = $u['id'];
                $this->M("files")->where('id','=',$u['id'])->update(['last_modify_time'=>time()]);
            }
            return pr(200,"上传成功",['src'=>$p,'file_id'=>$fid,'file_name'=>$file['name']]);
        }
    }

    /**大文件上传**/
    public function BigUpload(): Json
    {
        $chunkIndex = request()->post('which');//当前分片
        $totalChunk = request()->post('total');//总分片
        $chunkSize = request()->post('chunk_size');//分片大小
        $file = $_FILES['file'];
        $uid = $this->getUid(true);
        if($file['error']>0){
            return pr(300,"文件上传出错");
        }
        if(empty($uid)) return pr(304,'请先登陆');
        if($chunkSize * $chunkIndex > 1024 * 1024 * 1024) return pr(300,'文件过大，限制文件大小最大为1G');
        $arr = explode('.',$file['name']);
        if(count($arr)==0) return pr(301,"上传失败");
        else{
            $t = getSCType($file['name']);
            if($t == 0) return pr(302,'不支持的文件格式');
            $content = file_get_contents($file['tmp_name']);
            $p = saveToFile($file['name'],$content,$uid,$chunkIndex * $chunkSize);
            unlink($file['tmp_name']);
            $fid = 0;
            if($chunkIndex == $totalChunk - 1){//如果上传完成，则写入数据库
                $fileSize = filesize($p);
                $u = $this->M("files")->field('id')->where(['uid'=>$uid,'path'=>$p])->find();
                if(empty($u)){
                    $fid = $this->M("files")->insert(['size'=>$fileSize,'uid'=>$uid,'type'=>0,'path'=>$p,'name'=>$file['name'],'create_time'=>time(),'last_modify_time'=>time(),'down_times'=>0],true);
                }else{
                    $fid = $u['id'];
                    $this->M("files")->where('id','=',$u['id'])->update(['last_modify_time'=>time()]);
                }
            }
            return pr(200,"上传成功",['src'=>$p,'file_id'=>$fid,'file_name'=>$file['name']]);
        }
    }
}
